/*Clearing log and output files*/
proc datasets lib=work kill nolist memtype=data;
quit;
dm "clear log";
dm "clear output";
run;

/*Clearing the Results Viewer html window and saving the new output*/
ods html close;

libname source "";
libname output "";
libname cmf "";
libname asm "";
libname fk "";

options compress = yes;

/*Macro for Merging Two Datasets*/
%macro DR_merge(filename,file1,file2,mby);
	proc sort data = &file1;
		by &mby;
	proc sort data = &file2;
		by &mby;

	data &filename;
		merge &file1.(in=F1) &file2.(in=F2);
		by &mby;
		length merge_id $ 6;
		if F1 = 1 and F2 = 1 then merge_id = 'BOTH';
		if F1 = 1 and F2 = 0 then merge_id = 'MASTER';
		if F1 = 0 and F2 = 1 then merge_id = 'FILE2';

	proc freq data = &filename;
		tables merge_id / nocum;
		title "Merge Summary: MASTER(&file1) USING(&file2) OUTPUT(&filename) MERGING_BY(&mby)";

	proc sort data = &filename;
		by merge_id;
	run;
%mend;

/*Macro for renaming variables by adding a suffix following an underscore to the original variable name*/
%macro rename_suffix(oldvarlist, suffix);
	%let k=1;
	%let old = %scan(&oldvarlist, &k);
		%do %while("&old" NE "");
			rename &old = &old.&suffix;
				%let k = %eval(&k + 1);
			%let old = %scan(&oldvarlist, &k);
	%end;
%mend; 

/*Cleaning NAICS codes from early CMF years*/
%macro fkcompile(start,end);
%let year = &start.;
%do %until(&year.>&end.);
data fk;
	set fk.naics&year. (keep = lbdnum fk_naics02);
	rename lbdnum = lbdnum_c201002;
	rename fk_naics02 = fk_naics02_&year.;

%DR_merge(fk_all,fk_all,fk,lbdnum_c201002);
data fk_all;
	set fk_all (drop = merge_id);
run;
%let year = %eval(&year. + 1);
%end;
%mend;

%macro fkids(start,end);
%let year = &start.;
%do %until(&year.>&end.);
data fk_all;
	set fk_all;
	id_&year. = 0;
	if fk_naics02_&year. ne "" then id_&year. = 1;
run;
%let year = %eval(&year. + 1);
%end;
%mend;

%macro fkmacro(start);
%let year = &start.;
%let yearp1 = %eval(&year. + 1);
%let yearp2 = %eval(&year. + 2);
%let yearm1 = %eval(&year. - 1);
%let yearm2 = %eval(&year. - 2);

data fk_all;
	lbdnum_c201002 = "123456789123";
	if lbdnum_c201002 = "123456789123" then delete;
%fkcompile(&yearm2.,&yearp2.);
%fkids(&yearm2.,&yearp2.);

%let year = &start.;
%let yearp1 = %eval(&year. + 1);
%let yearp2 = %eval(&year. + 2);
%let yearm1 = %eval(&year. - 1);
%let yearm2 = %eval(&year. - 2);

data fk_all;
	set fk_all;
	fk_count = id_&yearm1.+id_&year.+id_&yearp1.;
	fk_count2 = id_&year.+id_&yearp1.+id_&yearp2.;
	fk_count3 = id_&year.+id_&yearm1.+id_&yearm2.;
	
	if fk_count = 3 and fk_naics02_&yearm1.=fk_naics02_&yearp1. and fk_naics02_&yearm1. ne fk_naics02_&year. 
		then fk_naics02=fk_naics02_&yearm1.;
	if fk_count = 3 and fk_naics02_&yearm1.=fk_naics02_&yearp1. and fk_naics02_&yearp1. ne fk_naics02_&year. 
		then fk_naics02=fk_naics02_&yearm1.;

	if fk_count2 = 3 and fk_naics02_&yearm1. = "" and fk_naics02_&year. ne fk_naics02_&yearp1. and 
	fk_naics02_&yearp1.=fk_naics02_&yearp2. then fk_naics02=fk_naics02_&yearp1.;

	if fk_count3 = 3 and fk_naics02_&yearp1. = "" and fk_naics02_&year. ne fk_naics02_&yearm1. and 
	fk_naics02_&yearm1.=fk_naics02_&yearm2. then fk_naics02=fk_naics02_&yearm1.;
	
	if fk_naics02 = "" then fk_naics02=fk_naics02_&year.;

data fk_&year.;
	set fk_all (keep = lbdnum_c201002 fk_naics02);
	if fk_naics02 = "" then delete;

run;
%mend;

data fk_all;
	lbdnum_c201002 = "123456789123";
	if lbdnum_c201002 = "123456789123" then delete;
%fkcompile(1976,1979);
%fkids(1976,1979);
data fk_all;
	set fk_all;
	fk_count = id_1976+id_1977+id_1978;
	fk_count2 = id_1977+id_1978+id_1979;
	
	if fk_count = 3 and fk_naics02_1976=fk_naics02_1978 and fk_naics02_1976 ne fk_naics02_1977 then fk_naics02=fk_naics02_1976;
	if fk_count = 3 and fk_naics02_1976=fk_naics02_1978 and fk_naics02_1978 ne fk_naics02_1977 then fk_naics02=fk_naics02_1976;

	if fk_count2 = 3 and fk_naics02_1976 = "" and fk_naics02_1977 ne fk_naics02_1978 and 
	fk_naics02_1978=fk_naics02_1979 then fk_naics02=fk_naics02_1978;
	
	if fk_naics02 = "" then fk_naics02=fk_naics02_1977;
	if fk_naics02 ne fk_naics02_1977 then change = 1;

data fk_1977;
	set fk_all (keep = lbdnum_c201002 fk_naics02);
	if fk_naics02 = "" then delete;

%fkmacro(1982);
%fkmacro(1987);
%fkmacro(1992);
%fkmacro(1997);
%fkmacro(2002);
%fkmacro(2007);


%macro compile_asm(start,end);
%let yyyy = &start.;
%do %until(&yyyy.>&end.);

%if &yyyy=1977 OR &yyyy=1982 OR &yyyy=1987 OR &yyyy=1992 
	 OR &yyyy=1997 OR &yyyy=2002 OR &yyyy=2007 %then %do;
data cmf;
	set cmf.cmf&yyyy.;
data cmf_tfp;
	set cmf.cmf&yyyy.tfpb1;
%DR_merge(cmf_all,cmf,cmf_tfp,lbdnum);

data cmf_all;
	set cmf_all (drop = merge_id);

data fk;
	set fk_&yyyy. (keep = lbdnum_c201002 fk_naics02);

%DR_merge(cmf_all,cmf_all,fk,lbdnum_c201002);

data cmf_all;
	set cmf_all;
	if fk_naics02 = "" then delete;
	if merge_id ne "BOTH" then delete;

data cmf_asm_final;
	set cmf_asm_final cmf_all(drop = merge_id);
%end;

%if &yyyy ne 1977 AND &yyyy ne 1982 AND &yyyy ne 1987 AND &yyyy ne 1992 
	 AND &yyyy ne 1997 AND &yyyy ne 2002 AND &yyyy ne 2007 %then %do;

data cmf;
	set asm.asm&yyyy.;
	
data cmf_tfp;
	set asm.asm&yyyy.tfpb1;

%DR_merge(cmf_all,cmf,cmf_tfp,lbdnum);

data cmf_all;
	set cmf_all (drop = merge_id);

data fk;
	set fk.naics&yyyy. (keep = lbdnum fk_naics02);
	rename lbdnum = lbdnum_c201002;

%DR_merge(cmf_all,cmf_all,fk,lbdnum_c201002);

data cmf_all;
	set cmf_all;
	if fk_naics02 = "" then delete;
	if merge_id ne "BOTH" then delete;

data cmf_asm_final;
	set cmf_asm_final cmf_all(drop = merge_id);
%end; 
%let yyyy = %eval(&yyyy. + 1);
%end;
%mend;

data cmf_asm_final;

%compile_asm(1977,2001);

proc export data = cmf_asm_final
	outfile = "/FILE PATH GOES HERE/CMF_ASM_1977_2001.dta"
	dbms = stata replace;
run;


%sysexec %str(cd /FILE PATH GOES HERE//;
	gzip CMF_ASM_1977_2001.dta;);

run;

data cmf_asm_final;

%compile_asm(2002,2009);

proc export data = cmf_asm_final
	outfile = "/FILE PATH GOES HERE/CMF_ASM_2002_2009.dta"
	dbms = stata replace;
run;


%sysexec %str(cd /FILE PATH GOES HERE/;
	gzip CMF_ASM_2002_2009.dta;);

run;

	
run;